<template>
  <div class="product-detail">
    <div class="header">
      <Header />
    </div>
    
    <el-row :gutter="30" class="product-content">
      <el-col :span="10">
        <el-image 
          :src="goods.goods_icon" 
          :alt="goods.goods_desc" 
          class="product-img"
          fit="contain"
        ></el-image>
      </el-col>
      
      <el-col :span="14">
        <div class="product-info">
          <h1 class="product-title">{{ goods.goods_desc }}</h1>
          
          <div class="product-price">
            售价: ¥{{ goods.goods_price.toFixed(2) }}
          </div>
          
          <div class="product-stock">
            库存: {{ goods.goods_stock_count }} 件
          </div>
          
          <div class="product-sales">
            销量: {{ goods.goods_sale_count }} 件
          </div>
          
          <div class="quantity-selector" v-if="goods.goods_stock_count > 0">
            <span class="quantity-label">购买数量:</span>
            <el-input-number
              v-model="quantity"
              :min="1"
              :max="goods.goods_stock_count"
              @change="handleQuantityChange"
              class="quantity-input"
            ></el-input-number>
          </div>
          
          <div class="quantity-selector" v-else-if="goods.goods_stock_count === 0">
            <span class="quantity-label">购买数量:</span>
            <span class="out-of-stock">商品已售罄</span>
          </div>
          
          <el-button 
            type="primary" 
            size="large" 
            class="add-cart-btn"
            :disabled="goods.goods_stock_count === 0"
            @click="handleAddToCart"
          >
            {{ goods.goods_stock_count === 0 ? '已售罄' : '加入购物车' }}
          </el-button>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup name="Product">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from '../utils/axios'
import Header from '../components/Header.vue'
import useCartStore from '../store/cart'

const route = useRoute()
const router = useRouter()
const cartStore = useCartStore()
const goods = ref({
  id: 0,
  goods_desc: '',
  goods_icon: '',
  goods_price: 0,
  goods_sale_count: 0,
  goods_stock_count: 0
})

const quantity = ref(1)

const fetchGoodsDetail = async () => {
  const goods_id = route.params.id
  try {
    const res = await axios.get(`/goods/detail?goods_id=${goods_id}`)
    if (res.code === 200) {
      goods.value = res.data
      quantity.value = Math.min(quantity.value, goods.value.goods_stock_count)
    } else {
      ElMessage.error('商品不存在')
      router.push('/home')
    }
  } catch (error) {
    console.error('获取详情失败:', error)
    ElMessage.error('加载详情失败')
    router.push('/home')
  }
}

const handleQuantityChange = (val) => {
  if (val < 1) {
    quantity.value = 1
  } else if (val > goods.value.goods_stock_count) {
    quantity.value = goods.value.goods_stock_count
    ElMessage.warning(`最多只能购买${goods.value.goods_stock_count}件`)
  }
}

const handleAddToCart = async () => {
  if (goods.value.goods_stock_count === 0) {
    ElMessage.warning('商品已售罄')
    return
  }
  
  if (quantity.value < 1 || quantity.value > goods.value.goods_stock_count) {
    ElMessage.warning('请选择有效的购买数量')
    return
  }
  
  const result = await cartStore.addToCart(goods.value.id, quantity.value)
  if (result.success) {
    ElMessage.success(`成功加入${quantity.value}件商品到购物车`)
  } else {
    ElMessage.error(result.message)
  }
}

onMounted(() => {
  fetchGoodsDetail()
})
</script>

<style scoped>
.product-content {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin: 20px auto;
  max-width: 1200px;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 1000;
}

.product-img {
  width: 100%;
  height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f8f9fa;
  border: 1px solid #eee;
  border-radius: 8px;
}



.product-info {
  padding: 24px;
}

.product-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
  line-height: 1.4;
}

.product-price {
  font-size: 20px;
  color: #ff4d4f;
  margin-bottom: 12px;
  font-weight: 500;
}

.product-stock,
.product-sales {
  margin-bottom: 8px;
  font-size: 16px;
}

.product-stock {
  color: #409eff;
}

.product-sales {
  color: #f56c6c;
}

.quantity-selector {
  margin: 20px 0;
  display: flex;
  align-items: center;
}

.quantity-label {
  font-size: 16px;
  margin-right: 10px;
  color: #333;
}

.quantity-input {
  width: 120px;
}

/* 新增：已售罄样式 */
.out-of-stock {
  color: #f56c6c;
  font-size: 14px;
  padding: 5px 0;
}

.product-desc h3 {
  font-size: 18px;
  font-weight: 500;
  color: #333;
  margin: 20px 0 10px;
}

.product-desc p {
  line-height: 1.6;
  color: #666;
  margin-bottom: 8px;
}

.add-cart-btn {
  margin-top: 20px;
  font-size: 16px;
  padding: 12px 32px;
}
</style>
